//
//  PullView.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/11/11.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "PullView.h"
#import "YYPullCell.h"

@interface PullView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,strong)NSArray * dataArr;
@property (nonatomic ,strong)UICollectionView * collectionView;

@end

@implementation PullView

+ (PullView *)sharedInstance{
    static  PullView * pullView;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        pullView = [[PullView alloc] init];
    });
    return pullView;
}

- (instancetype)init{
    if (self = [super init]) {
        [self creatUI];
    }
    return self;
}

-(instancetype) initWithFrame:(CGRect)frame dataArr:(NSArray *)array{
    if (self = [super initWithFrame:frame]) {
        self.dataArr = array;
        [self creatUI];
    }
    return self;
}

+ (void)pullWithArr:(NSArray *)dataArr frme:(CGRect)frame superView:(UIView *)superView index:(selectedBlock)block{
    [[PullView sharedInstance] pullViewWithData:dataArr frame:frame superView:superView index:(selectedBlock)block];
}

- (void)pullViewWithData:(NSArray *)arr frame:(CGRect)frame superView:(UIView *)superView index:(selectedBlock)block{
    _dataArr = arr;
    _block = block;
     self.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, (self.dataArr.count+2)/3*50);
    [self.collectionView reloadData];
    [superView addSubview:self];
    
}

- (void)creatUI{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    //设置CollectionView的属性
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.scrollEnabled = NO;
    collectionView.backgroundColor = [UIColor whiteColor];
    //注册Cell
    [collectionView registerClass:[YYPullCell class] forCellWithReuseIdentifier:@"cell"];
    [self addSubview:collectionView];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    self.collectionView = collectionView;
    
}


#pragma mark  设置CollectionView每组所包含的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
    
}

#pragma mark  设置CollectionCell的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    YYPullCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    cell.titleLabel.text = self.dataArr[indexPath.row][@"type"];
    if (_selIndex == indexPath.row) {
        cell.titleLabel.textColor = [UIColor colorWithHexString:@"01c8fc"];
    }else{
         cell.titleLabel.textColor = [UIColor colorWithHexString:@"666666"];
    }
    return cell;
}

#pragma mark  定义每个UICollectionView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return  CGSizeMake(DEF_SCREEN_WIDTH()/3,50);
}



//#pragma mark  定义整个CollectionViewCell与整个View的间距
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
//{
//    return UIEdgeInsetsMake(0, 0, (self.medals.count / 3 - 2) * DeviceSize.width / 3, 0);//（上、左、下、右）
//}


#pragma mark  定义每个UICollectionView的横向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark  定义每个UICollectionView的纵向间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

#pragma mark  点击CollectionView触发事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    _selIndex = indexPath.row;
    if (_block) {
        _block(indexPath.row);
    }
    [collectionView reloadData];
    [self removeFromSuperview];
    NSLog(@"---------------------");
}

@end
